package com.hsw.habit.controller;

import com.hsw.habit.frame.annotation.CurrentUser;
import com.hsw.habit.frame.annotation.UserId;
import com.hsw.habit.frame.utils.ToolUtils;
import com.hsw.habit.model.JsonData;
import com.hsw.habit.model.TaskGroups;
import com.hsw.habit.model.TaskItem;
import com.hsw.habit.model.User;
import com.hsw.habit.service.GroupService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/group")
public class GroupController {

    @Resource
    GroupService groupService;

    @RequestMapping("/index")
    public JsonData index(@CurrentUser User user) {
        return JsonData.success(groupService.selectGroupsByUserId(user.getId()));
    }

    @RequestMapping("/add")
    public JsonData add(@CurrentUser User user,TaskGroups taskGroups) {
        if (null == taskGroups) {
            return JsonData.error();
        }

        taskGroups.setUserId(user.getId());
        boolean res = groupService.insertGroup(taskGroups);
        if (!res) {
            return JsonData.error();
        }
        return JsonData.success();
    }

    @RequestMapping("/update")
    public JsonData update(TaskGroups taskGroups) {
        if (null == taskGroups) {
            return JsonData.error();
        }

        TaskGroups groups = groupService.selectGroupById(taskGroups.getId());

        if (null != groups) {
            groups.setGroupName(taskGroups.getGroupName());
        }

        boolean res = groupService.updateGroup(groups);
        if (!res) {
            return JsonData.error();
        }
        return JsonData.success();
    }

    @RequestMapping("/remove")
    public JsonData remove(int id) {
        if (id <= 0) {
            return JsonData.error();
        }
        boolean res = groupService.deleteGroup(id);
        if (!res) {
            return JsonData.error();
        }
        return JsonData.success();
    }
}
